package com.itmayiedu.service.impl;

import com.itmayiedu.model.test.Student;
import com.itmayiedu.model.test.StudentMapper;
import com.itmayiedu.model.xmcyz.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * Created by Devin on 2018/1/22.
 */
@Service
public class StudentServiceImpl {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Transactional //注意这里就是利用了atomikos+jta的多数据源的事物
    public int add(String name,String sex,Timestamp createTime){
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        teacherMapper.insert("萧景睿","男",timestamp);
        int i = 1/0;
        studentMapper.insert(name,sex,createTime);
       return 0;
    }

    public Student findById(Integer id){
        return studentMapper.findById(id);
    }


    @Async //异步注解相当于将doAsnc里面的方法扔到了子线程里面去执行，相当于重新开了一个子线程
    public void doAsync() {

        System.out.println("###========doAsync========## 2");

        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("========"+i);
            }catch (Exception e){

            }
        }
        System.out.println("###========doAsync========## 3");
    }
}
